# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=freetype
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.5
pkgrel=1
pkgdesc="TrueType font rendering library (mingw-w64)"
arch=('any')
url="http://www.freetype.org/"
license=(GPL2+ custom:FreeType)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!libtool' 'staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/freetype/freetype-${pkgver}.tar.bz2
        freetype-2.2.1-enable-valid.patch
        freetype-2.5.1-enable-spr.patch
        freetype-2.5.1-enable-sph.patch
        mingw-config.patch)
sha256sums=('e20a6e1400798fd5e3d831dd821b61c35b1f9a6465d6b18a53a9df4cf441acf0'
            '54c83a91b0b2ad7edad7df00a2c26a11ca18431a8e323db9471268a139c46f7e'
            '3ee5690d4d7cb48b8862e203d09b530805066cdfda0f51716b8ae2a47eef8b2d'
            '324012ad68388d22c63368314721d2360b48dd40daa808d0383699a281d931b0'
            'fcfe98e57b6afd449f6d7464c92199998254b5a6becebc20f9eb06f1667138b2')

prepare() {
  cd "${srcdir}/freetype-${pkgver}"
  patch -p1 -i "${srcdir}/freetype-2.2.1-enable-valid.patch"
  patch -p1 -i "${srcdir}/freetype-2.5.1-enable-spr.patch"
  patch -p1 -i "${srcdir}/mingw-config.patch"

  # Disabled for now due to resistance
  # Kept here for easier rebuilds via ABS
  # https://bugs.archlinux.org/task/35274
  #patch -Np1 -i "${srcdir}/freetype-2.5.1-enable-sph.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  ../freetype-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-static \
    --enable-shared \
    --with-zlib \
    --with-bzip2 \
    --with-png \
    --with-harfbuzz

  make
}

package () {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install

  # Replace the absolute path.
  sed -i "s|$(cygpath -m /)|/|g" ${pkgdir}${MINGW_PREFIX}/bin/freetype-config

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/GPLv2.TXT"   "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GPLv2.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/FTL.TXT"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/FTL.TXT"
}
